/**
 * @file		init_hw.c
 * @brief		Hardware Initialization.
 * @note		None
 * @attention	None
 * 
 * <B><I>ALL RIGHTS RESERVED, COPYRIGHT&copy; SOCIONEXT INCORPORATED 2016</I></B>
 */

// PMU Register
#define PMU_CTL			(0x4DFFF000)	// Power control register.
#define PMU_INT_EN		(0x4DFFF008)	// Interrupt flag enable register.
#define PMU_INT_ST		(0x4DFFF00C)	// Interrupt flag register.
#define PMU_INT_CLR		(0x4DFFF010)	// Interrupt flag clear register.
#define PWR_STR			(0x4DFFF018)	// Power status register.
#define PWR_ON_EN0		(0x4DFFF020)	// Power ON reserve#0 register.
#define PWR_ON_CYC1		(0x4DFFF054)	// Power ON cycle register#1.
#define PWR_DLY_CTL1	(0x4DFFF084)	// Priority register#1.
#define PWR_TM_CTL0		(0x4DFFF0A0)	// Timing control register#0.
#define PWR_TM_S1_CTL0	(0x4DFFF0B0)	// Timing control register#0 for PMU Strong1.
#define PWR_TM_S2_CTL0	(0x4DFFF0C0)	// Timing control register#0 for PMU Strong2.

// Grobal Function

void vInitHW()
{
	//--------------------------------------------------------------------------
	// Power Management
	//--------------------------------------------------------------------------
	// Set PSW On time of On chip PG.
	*((volatile unsigned long*)PWR_TM_CTL0)			= 0x00000000;
	*((volatile unsigned long*)PWR_TM_S1_CTL0)		= 0x00000000;
	*((volatile unsigned long*)PWR_TM_S2_CTL0)		= 0x02210200;
	// Set Power On to Clamp Cancel time of Off chip PG.
	*((volatile unsigned long*)PWR_ON_CYC1)			= 0xF0000000;		// Provisional value.
	
	// Off chip PG Power on.
	// check status.
	if ((*((volatile unsigned long*)PWR_STR) & 0x00000080) != 0x00000080) {
		// Power of PG7 is OFF.
		
		// PG7 : Set Power supply.
		*((volatile unsigned long*)PWR_ON_EN0)		= 0x00000080;
		// PG7 : Set Power ON timing.
		*((volatile unsigned long*)PWR_DLY_CTL1)	= 0x00FFFFFF;		// PWR_DLY_CTL1.PON7 clear.
		*((volatile unsigned long*)PWR_DLY_CTL1)	|= 0x1F000000;		// PWR_DLY_CTL1.PON7 set.
		// Power-switch-ON done Interrupt Clear.
		*((volatile unsigned long*)PMU_INT_CLR)		= 0x00000010;
		// Power-switch-ON done Interrupt Enable.
		*((volatile unsigned long*)PMU_INT_EN)		= 0x00000010;
		
		// Power Up SET.
		*((volatile unsigned long*)PMU_CTL)			|= 0x0000AA01;
		
		// Wait for the power to stabilize.
		while((*((volatile unsigned long*)PMU_INT_ST) & 0x00000010) != 0x00000010) {
			// wait.
		}
		
		// Power-switch-ON done Interrupt Clear.
		*((volatile unsigned long*)PMU_INT_CLR)		= 0x00000010;
	}
}

